import psycopg2
 
try:
    # 连接参数
    conn_params = f"host='localhost' dbname='crms' user='postgres' password='jcc123456'"
    # 建立连接
    conn = psycopg2.connect(conn_params)
    print("Connected to the database")
    # 创建cursor对象
    cur = conn.cursor()
    # 执行SQL查询
    cur.execute("select * from course.course_categories")
    # 获取查询结果
    record = cur.fetchone()
    print("You are connected to - ", record)
except Exception as e:
    print("Unable to connect to the database")
    print(e)
finally:
    # 关闭cursor和连接
    if cur:
        cur.close()
    if conn:
        conn.close()